package bavaria.hightech.banking.accounts;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Calendar;

import bavaria.hightech.banking.conditions.FixedDepositConditions;
import bavaria.hightech.banking.exceptions.FixedDepositException;
import bavaria.hightech.banking.money.Currency.currencyEnum;
import bavaria.hightech.banking.resourceBundles.LanguageBundle;
import bavaria.hightech.banking.time.TimeServer;

/**
 * Fixed Deposit Account
 * 
 * @author Dietmar
 */
public class FixedDepositAccount extends Account {

	private final Calendar expirationDate;
	private FixedDepositConditions act_condition;
	private boolean interestGiven = false;

	/**
	 * Default-Konstruktor FixedDeposit Account with depositor, condition
	 * 
	 * @param accNr
	 * @param depositor
	 * @param interest
	 */
	public FixedDepositAccount(int accNr, String depositor,
			FixedDepositConditions act_condition, currencyEnum currency,
			LanguageBundle languageBundle) {
		super(accNr, depositor, currency);
		this.act_condition = act_condition;
		expirationDate = (Calendar) creationTimeStamp.clone();
		expirationDate.add(Calendar.MONTH, this.act_condition.getTime());
		this.languageBundle = languageBundle;
	}

	/**
	 * calculates interest without check for pos/neg
	 */
	@Override
	public void applyInterest() {
		if (TimeServer.getCalendar().after(expirationDate) && !interestGiven) {
			addAmount(currencyEnum.EURO,
					(long) (getBalance() / 100 * getInterest_Have()), "Zinsen");
			interestGiven = true;
		}
	}

	/**
	 * draws amount if balance will not be below 0 books or throws exception
	 * 
	 * @throws FixedDepositException
	 */
	@Override
	public boolean drawAmount(currencyEnum currency, long amount, String reason)
			throws FixedDepositException {
		if ((getBalance() - amount) >= 0) {
			super.drawAmount(currencyEnum.EURO, amount, reason);
		} else {
			throw new FixedDepositException("FixedDeposit account below 0");
		}
		return true;
	}

	/**
	 * returns string with acc type, creation date, depositor, acc nr, balance,
	 * condition
	 */
	@Override
	public String toString() {
		String str = "";
		String formattedBalance = NumberFormat.getNumberInstance(
				languageBundle.getLocale()).format(getBalance());
		String formattedDate = DateFormat.getDateInstance(DateFormat.MEDIUM,
				languageBundle.getLocale()).format(creationTimeStamp.getTime());
		str = "{0}: {2}\n";
		str += "{3}: " + formattedDate + "\n";
		str += "{4}: " + getDepositor() + "\n";
		str += "{18}: " + getAccountNumber() + "\n";
		str += "{5}: " + formattedBalance + "\n";
		str += act_condition;
		str = MessageFormat.format(str, languageBundle.getContents());
		return str;
	}

	/**
	 * get interest have
	 * @return
	 */
	public float getInterest_Have() {
		return (act_condition).getInterest_Have();
	}

	/**
	 * get expiration date
	 * @return
	 */
	public Calendar getExpirationDate() {
		return expirationDate;
	}

	/**
	 * get condition
	 * @return
	 */
	public FixedDepositConditions getCondition() {
		return act_condition;
	}
}
